package utils;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * @author Mapull
 * @date 2021/10/28
 * @description 验证码
 */
public class Vercode {

    public static void main2(String[] args) throws Exception {
//        String url = "http://117.39.28.234:8023/vercode/vercode.php?a=0.820979319410611";
//        OkHttpClient httpClient = new OkHttpClient();
//        Request request = new Request.Builder().url(url).build();
//        Call call = httpClient.newCall(request);
//        Response response = call.execute();
//        ResponseBody body = response.body();
//        InputStream inputStream = body.byteStream();
//
//        Files.copy(inputStream, Paths.get("./temp.gif"));
//        Tesseract tesseract = new Tesseract();
//        tesseract.setDatapath("./tessdata");
//        File file = Paths.get("temp.gif").toFile();
//        System.out.println(file.getAbsolutePath());
//        System.out.println(file.exists());
//        String s = tesseract.doOCR(file);
//        System.out.println(s);

    }

//    public static void main(String[] args) {
//        Ocr.setUp(); // one time setup
//        Ocr ocr = new Ocr(); // create a new OCR engine
//        ocr.startEngine("eng", Ocr.SPEED_SLOW); // English
//        File file = Paths.get("temp.gif").toFile();
//        String s = ocr.recognize(new File[] {file},
//                Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT); // PLAINTEXT | XML | PDF | RTF
//        System.out.println("Result: " + s);
//        ocr.stopEngine();
//    }

    public static void main(String[] args) {
        //原始验证码地址
        String originalImg = "D:\\temp.gif";
        //识别样本输出地址
        String ocrResult = "D:\\temp.jpg";
        removeBackground(originalImg, ocrResult);

//        ImgUtils.cuttingImg(ocrResult);
        executeParse();

    }
    public static void executeParse(){
        Tesseract tesseract = new Tesseract();
        tesseract.setDatapath("./tessdata");
        File file = Paths.get("D:\\temp.jpg").toFile();
        System.out.println(file.getAbsolutePath());
        System.out.println(file.exists());
        String s = null;
        try {
            s = tesseract.doOCR(file);
        } catch (TesseractException e) {
            e.printStackTrace();
        }
        System.out.println(s);
    }

    public static void removeBackground(String imgUrl, String resUrl){
        //定义一个临界阈值
        int threshold = 300;
        try{
            BufferedImage img = ImageIO.read(new File(imgUrl));
            int width = img.getWidth();
            int height = img.getHeight();
            for(int i = 1;i < width;i++){
                for (int x = 0; x < width; x++){
                    for (int y = 0; y < height; y++){
                        Color color = new Color(img.getRGB(x, y));
                        System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
                        int num = color.getRed()+color.getGreen()+color.getBlue();
                        if(num >= threshold){
                            img.setRGB(x, y, Color.WHITE.getRGB());
                        }
                    }
                }
            }
            for(int i = 1;i<width;i++){
                Color color1 = new Color(img.getRGB(i, 1));
                int num1 = color1.getRed()+color1.getGreen()+color1.getBlue();
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Color color = new Color(img.getRGB(x, y));

                        int num = color.getRed()+color.getGreen()+color.getBlue();
                        if(num==num1){
                            img.setRGB(x, y, Color.BLACK.getRGB());
                        }else{
                            img.setRGB(x, y, Color.WHITE.getRGB());
                        }
                    }
                }
            }
            File file = new File(resUrl);
            if (!file.exists())
            {
                File dir = file.getParentFile();
                if (!dir.exists())
                {
                    dir.mkdirs();
                }
                try
                {
                    file.createNewFile();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            ImageIO.write(img, "jpg", file);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void cuttingImg(String imgUrl){
        try{
            File newfile=new File(imgUrl);
            BufferedImage bufferedimage=ImageIO.read(newfile);
            int width = bufferedimage.getWidth();
            int height = bufferedimage.getHeight();
            if (width > 52) {
                bufferedimage= utils.ImgUtils.cropImage(bufferedimage,(int) ((width - 52) / 2),0,(int) (width - (width-52) / 2),(int) (height));
                if (height > 16) {
                    bufferedimage= utils.ImgUtils.cropImage(bufferedimage,0,(int) ((height - 16) / 2),52,(int) (height - (height - 16) / 2));
                }
            }else{
                if (height > 16) {
                    bufferedimage= utils.ImgUtils.cropImage(bufferedimage,0,(int) ((height - 16) / 2),(int) (width),(int) (height - (height - 16) / 2));
                }
            }
            ImageIO.write(bufferedimage, "jpg", new File(imgUrl));
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}
